package com.mybatisplus.demo.collect;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @CLASSNAME CollectingIntoMap
 * @DESCRIPTION 描述
 * @AUTHOR chen
 * @DATE 2019-10-28 22:54
 **/

public class CollectingIntoMap {

    public static class Person {
        private int id;

        private String name;

        public Person(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Person{" + "id=" + id + ", name='" + name + '\'' + '}';
        }
    }

    public static Stream <Person> people() {
        return Stream.of( new Person( 1, "hello" ), new Person( 2, "hi" ) );
    }

    public static void main(String[] args) throws IOException {

        Map <Integer, String> idToName = people().collect( Collectors.toMap( Person::getId, Person::getName ) );
        System.out.println( idToName );

        Map <Integer, Person> idToPerson = people().collect( Collectors.toMap( Person::getId, Function.identity() ) );
        System.out.println( idToPerson );

        idToPerson = people().collect( Collectors.toMap( Person::getId, Function.identity(), (exist, newValue) -> {
            throw new IllegalStateException();
        }, TreeMap::new ) );

        System.out.println(idToPerson);
    }


}
